import requests
import unittest
from unit__001.unit_02.Mysql import Mysql


class A3mall(unittest.TestCase):
    def setUp(self) -> None:
        print('start case')
        # 初始化数据库
        self.db = Mysql(host='10.59.9.12', user='root', password='yidong666', database='a3mall')

    def tearDown(self) -> None:
        print('end case')
        # 调用关闭数据库的方法
        self.db.close()

    token = None
    amount = None
    username = None

    # 登录
    def test_A3mall_01(self):
        login = requests.post(url='http://10.59.9.14:7003/api/public/login',
                              json={"username": "18185603008", "password": "123456"})
        print(login.json())
        A3mall.token = login.json()['data']['token']
        print(A3mall.token)
        # # 提取用户登录时余额
        # A3mall.amount = login.json()['data']['amount']
        # print('登录时用户余额为:', A3mall.amount)
        # 提取username 数据库断言查询用
        # A3mall.username = login.json()['data']['username']
        # print('username:', A3mall.username)
        # userid = '331'
        # result = self.db.chaxun(f'select * from mall_users where username={"18185607777"}')
        # print(result)

    # 搜索
    def test_A3mall_02(self):
        search = requests.get(
            url='http://10.59.9.14:7003/api/search/list?page=1&keywords=%E5%B0%8F%E7%B1%B3&type=0&sort=1')

        print(search.json())

    # 商品详情
    spid = None

    def test_A3mall_03(self):
        view = requests.get(url='http://10.59.9.14:7003/api/goods/view?id=71')
        print(view.json())
        # 提取商品id
        A3mall.spid = view.json()['data']['goods']['id']
        print('商品id:', A3mall.spid)

    # 提交订单

    payable_amount = None

    def test_A3mall_04(self):
        confirm = requests.get(
            url='http://10.59.9.14:7003/api/order/confirm?id=71&type=buy&sku_id=&num=1&shipping_type=1',
            headers={'auth-token': 'Bearer ' + A3mall.token})
        print(confirm.json())
        # 提取用户购买商品需要支付的总余额
        A3mall.payable_amount = confirm.json()['data']['payable_amount']
        print('用户购买商品需要支付的总余额：', A3mall.payable_amount)

    order_id = None

    # 确认订单
    def test_A3mall_05(self):
        create = requests.post(url='http://10.59.9.14:7003/api/order/create',
                               headers={'auth-token': 'Bearer ' + A3mall.token},
                               json={
                                   "id": A3mall.spid,
                                   "type": "buy",
                                   "address_id": 56,
                                   "store_id": 1,
                                   "shipping_type": 1,
                                   "bonus_id": "0",
                                   "payment": "balance",
                                   "remarks": "",
                                   "source": 1,
                                   "url": "http://10.59.9.14:7003/wap/pages/cart/confirm?id=71&sku_id=&num=1&type=buy",
                                   "sku_id": "",
                                   "num": "1"
                               })
        print(create.json())
        # 提取订单id
        A3mall.order_id = create.json()['data']['order_id']
        print(A3mall.order_id)

    users_price = None

    # 订单详情
    def test_A3mall_06(self):
        print(A3mall.token)
        detail = requests.post(url='http://10.59.9.14:7003/api/order/detail',
                               headers={'auth-token': 'Bearer ' + A3mall.token},
                               json={"id": A3mall.order_id})
        print(detail.json())
        # 用户登录时的余额-购买商品的总价格=用户剩余的余额
        user_yue = float(A3mall.amount)-float(A3mall.payable_amount)
        print(user_yue)
        # 数据库断言 用户支付完成后的总余额与数据库的总余额是否一致
        result = self.db.chaxun(f'select amount from mall_users where username={A3mall.username}')
        print(float(result[0][0]))
        self.assertEqual(user_yue,float(result[0][0]))


if __name__ == '__main__':
    unittest.main()
